﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.IO;
using WindowsInput;
using System.Media;
namespace Kinect4Presentation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private KinectSensor sensore;
        private bool EseguitoManoSinistra = false;
        private bool EseguitoManoDestra = false;
        private bool EseguitoAudioTracciamento = false;

        public MainWindow()
        {
            InitializeComponent();

            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (KinectSensor possibileSensore in KinectSensor.KinectSensors)
            {
                if (possibileSensore.Status == KinectStatus.Connected)
                {
                    this.sensore = possibileSensore;
                    break;
                }
            }
            if (this.sensore != null)
            {
                this.sensore.SkeletonStream.Enable();
                this.sensore.SkeletonFrameReady += this.RilevamentoScheletroPronto;
                try
                {
                    this.sensore.Start();
                }
                catch (IOException)
                {

                    this.sensore = null;
                }
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.sensore != null)
            {
                this.sensore.Stop();
            }
        }

        private void RilevamentoScheletroPronto(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] scheletri = new Skeleton[0];
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    scheletri = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(scheletri);
                }
            }
            if (scheletri.Length != 0)
            {
                foreach (Skeleton scheletro in scheletri)
                {
                    if (scheletro.TrackingState == SkeletonTrackingState.Tracked)
                    {
                        if ( (scheletro.Joints[JointType.Head].TrackingState == JointTrackingState.Tracked) )
                        {
                            if (EseguitoAudioTracciamento != true)
                            {
                                AudioManager.PlayStartTracking();
                                EseguitoAudioTracciamento = true;

                                //lblTesta.Content = "Non Tracciata";
                            }
                        }
                        else
                        {
                            EseguitoAudioTracciamento = false;
                            //lblTesta.Content = "Non Tracciata";
                        }
                        if (ManoSinistraAlzata(scheletro.Joints[JointType.HandLeft].Position, scheletro.Joints[JointType.ShoulderLeft].Position))
                        {
                            if (EseguitoManoSinistra != true)
                            {
                                InputSimulator.SimulateKeyPress(VirtualKeyCode.LEFT);
                                
                                //AudioManager.PlayChangePage();
                                EseguitoManoSinistra = true;
                            }
                        }
                        else
                        {
                            this.EseguitoManoSinistra = false;
                        }
                        if (ManoDestraAlzata(scheletro.Joints[JointType.HandRight].Position, scheletro.Joints[JointType.ShoulderRight].Position))
                        {
                            if (EseguitoManoDestra != true)
                            {
                                InputSimulator.SimulateKeyPress(VirtualKeyCode.RIGHT);
                                //AudioManager.PlayChangePage();
                                EseguitoManoDestra = true;
                            }
                        }
                        else
                        {
                            this.EseguitoManoDestra = false;
                        }
                    }
                }
            }
        }

        private bool ManoSinistraAlzata(SkeletonPoint posizioneManoSinistra, SkeletonPoint posizioneSpallaSinistra)
        {
            if (posizioneManoSinistra.Y > posizioneSpallaSinistra.Y)
                return true;
            else
                return false;
        }
        private bool ManoDestraAlzata(SkeletonPoint posizioneManoDestra, SkeletonPoint posizioneSpallaDestra)
        {
            if (posizioneManoDestra.Y > posizioneSpallaDestra.Y)
                return true;
            else
                return false;
        }


        
    }
}
